package com.lanou.horo.zhuizhui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.horo.zhuizhui.adapter.SelectedAlbumAdapter;
import com.lanou.horo.zhuizhui.entity.SelectedAlbumItem;
import com.lanou.horo.zhuizhui.utility.recyclerView.DividerItemDecoration;
import com.lanou.horo.zhuizhui.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/27.
 * 专辑
 */
public class SelectedAlbumActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private SelectedAlbumAdapter adapter;
    private RequestQueue queue;
    private ImageView backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_selected);
        initView();
        initData();
    }

    private void initView() {
        backBtn = (ImageView) findViewById(R.id.backBtn);
        backBtn.setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.selectedAlbumRecyclerView);
        adapter = new SelectedAlbumAdapter(this);
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
    }

    private void initData() {
        queue = Volley.newRequestQueue(this);
        String url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=1446079638062&param=%7B%22id%22%3A836%2C%22v%22%3A1%2C%22t%22%3A0%2C%22count%22%3A18%2C%22page%22%3A1%7D&sig=9b0ccb102c721bf62e97420a3433854c";

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SelectedAlbumItem sai = gson.fromJson(response , SelectedAlbumItem.class);
                if (sai != null ){
                    List<SelectedAlbumItem.ContentListsEntity> list = sai.getContent_lists();
                    if (list != null && list.size() > 0){
                        adapter.addConList(list);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backBtn:
                finish();
                break;
        }
    }
}
